package com.example.shirospringboot.service.impl;

import com.example.shirospringboot.dao.UserDao;
import com.example.shirospringboot.pojo.User;
import com.example.shirospringboot.service.UserService;
import com.example.shirospringboot.utils.Saltutils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public void register(User user) {
        //处理业务调用dao
        //1.生成随机盐
        String salt= Saltutils.getSalt(8);
        //将随机盐保存到数据库
        user.setSalt(salt);
        //使用铭文密码进行md5和salt  hash散列
        Md5Hash md5Hash=new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    @Override
    public User findByName(String name) {
        User salt=userDao.findByName(name);
        return salt;
    }

    @Override
    public User findByUserName(String name) {
        User user=userDao.findByUserName(name);
        return user;
    }

}
